const backgroundColor='#45415a';


var currentDateData={};
var currentMonthData={};
var orderInfo=[];
//单位:秒
const scrollTime=4;
const requestTimeInterval=5;
initNoticeBoard();
setVueDataTimer();

initChart();


function initChart(){
	var directory="/tlwork/Public/Home/data/";
	
	var leftTopPromise=ajaxRequestPromise(directory+"left-top.txt");
	var leftBottomPromise=ajaxRequestPromise(directory+"left-bottom.txt");
	var rightTopPromise=ajaxRequestPromise(directory+"right-top.txt");
	var rightBottomPromise=ajaxRequestPromise(directory+"right-bottom.txt");
	var chinaMap=ajaxRequestPromise(directory+"chinaMap.txt");
	var p = Promise.all([leftTopPromise,leftBottomPromise,rightTopPromise,rightBottomPromise,chinaMap]);
	p.then(function(list) {
	    console.log(list);
	    
	    createLeftTopChart(list[0]);
	    createleftBottomChart(list[1]);
	    createRightTopChart(list[2]);
	    createRightBottomChart(list[3]);
	    createChinaMapChart(list[4]);
	});
	
}




function initNoticeBoard(){
	var directory="/tlwork/Public/Home/data/";
	
	
	var orderInfo=ajaxRequestPromise(directory+"orderInfo.txt");
	var dateData=ajaxRequestPromise(directory+"dateData.txt");
	var monthData=ajaxRequestPromise(directory+"monthData.txt");

	var p = Promise.all([orderInfo,dateData,monthData]);
	p.then(function(list) {
	    console.log(list);
	    
	    initOrderInfo(list[0]);
	    initCurrentDateData(list[1]);
	    initCurrentMonthData(list[2]);
	    
	    initScrollbarVueData();
		setNoticeBoardVueData();

	});
	
	
	
	
}


function initScrollbarVueData(){
	var currentTimeStamp=Date.parse(new Date());
	var initData=[];
	for(var i=0;i<orderInfo.length;i++){
		var orderInfoItemTimeStamp=orderInfo[i].timeStamp;
		if(currentTimeStamp<orderInfoItemTimeStamp){
			initData.push(orderInfo[i-1]);
			initData.push(orderInfo[i-2]);
			break;
		}
	}
	scrollbarVue.list=initData;
}

function getCurrentDateSumDealMoney(){
	var currentDateTimeStamp=Date.parse(new Date());
	var currentDateSumDealMoney=0;
	for(var i=0;i<orderInfo.length;i++){
		var orderInfoItem=orderInfo[i];
		if(orderInfoItem.timeStamp<=currentDateTimeStamp){
			currentDateSumDealMoney+=parseFloat(orderInfoItem.totalMoney);
		}else{
			break;
		}
	}
	return currentDateSumDealMoney;
}

function getCurrentDateSumOrderQuantity(){
	var currentDateTimeStamp=Date.parse(new Date());
	var currentDateSumOrderQuantity=0;
	for(var i=0;i<orderInfo.length;i++){
		var orderInfoItem=orderInfo[i];
		if(orderInfoItem.timeStamp<=currentDateTimeStamp){
			currentDateSumOrderQuantity+=1;
		}else{
			break;
		}
	}
	return currentDateSumOrderQuantity;
}

//时间范围(startTimeStamp,endTimeStamp]
function searchTimeStampRangeData(startTimeStamp,endTimeStamp){
	var data=[];
	for(var i=0;i<orderInfo.length;i++){
		var orderInfoItem=orderInfo[i];
		var orderInfoItemTimeStamp=orderInfoItem.timeStamp;
		if(startTimeStamp<orderInfoItemTimeStamp){
			if(orderInfoItemTimeStamp<=endTimeStamp){
				data.push(orderInfoItem);
			}
		}
		if(endTimeStamp<orderInfoItemTimeStamp){
			break;
		}
	}
	return data;
}



//滚动栏数据
var scrollbarVue=new Vue({
	el:"#scrollbar",
	data:{
		list:[]
	}
});


var noticeBoardVue=new Vue({
	el:"#notice-board",
	data:{
		currentDateSumDealMoney:0,
		currentMonthSumDealMoney:0,
		currentYearSumDealMoney:0,
		
		currentDateSumOrderQuantity:0,
		currentMonthSumOrderQuantity:0,
		currentYearSumOrderQuantity:0,
		
		
		
		tweenedCurrentDateSumDealMoney:0,
		tweenedCurrentMonthSumDealMoney:0,
		tweenedCurrentYearSumDealMoney:0,
		
		tweenedCurrentDateSumOrderQuantity:0,
		tweenedCurrentMonthSumOrderQuantity:0,
		tweenedCurrentYearSumOrderQuantity:0,
		
		
		show:true
	},
	computed: {
        animatedCurrentDateSumDealMoney: function() {
            return this.numFormat(this.tweenedCurrentDateSumDealMoney.toFixed(0));
        },
        animatedCurrentMonthSumDealMoney: function() {
            return this.numFormat(this.tweenedCurrentMonthSumDealMoney.toFixed(0));
        },
        animatedCurrentYearSumDealMoney: function() {
            return this.numFormat(this.tweenedCurrentYearSumDealMoney.toFixed(0));
        },
        
        animatedCurrentDateSumOrderQuantity: function() {
            return this.numFormat(this.tweenedCurrentDateSumOrderQuantity.toFixed(0));
        },
        animatedCurrentMonthSumOrderQuantity: function() {
            return this.numFormat(this.tweenedCurrentMonthSumOrderQuantity.toFixed(0));
        },
        animatedCurrentYearSumOrderQuantity: function() {
            return this.numFormat(this.tweenedCurrentYearSumOrderQuantity.toFixed(0));
        },
    },
    watch: {
        currentDateSumDealMoney: function(newValue) {
            TweenLite.to(this.$data, 2, { tweenedCurrentDateSumDealMoney: newValue });
        },
        currentMonthSumDealMoney: function(newValue) {
            TweenLite.to(this.$data, 2, { tweenedCurrentMonthSumDealMoney: newValue });
        },
        currentYearSumDealMoney: function(newValue) {
            TweenLite.to(this.$data, 2, { tweenedCurrentYearSumDealMoney: newValue });
        },
        
        currentDateSumOrderQuantity: function(newValue) {
            TweenLite.to(this.$data, 1, { tweenedCurrentDateSumOrderQuantity: newValue });
        },
        currentMonthSumOrderQuantity: function(newValue) {
            TweenLite.to(this.$data, 1, { tweenedCurrentMonthSumOrderQuantity: newValue });
        },
        currentYearSumOrderQuantity: function(newValue) {
            TweenLite.to(this.$data, 1, { tweenedCurrentYearSumOrderQuantity: newValue });
        }
    },
	methods:{
    	numFormat:function(num){
    		var numStr=String(num);
			var newNumStr=numStr;
			if(numStr.length>3){
				var numStr=numStr.split("").reverse().join("");
				var newNumStr=numStr[0];
			
				for(var i=1;i<numStr.length;i++){
					if(i%3==0){
						newNumStr+=","+numStr[i];
					}else{
						newNumStr+=numStr[i];
					}
				}
				newNumStr=newNumStr.split("").reverse().join("");
			}
			
			return newNumStr;
		}
    }
});
